<template>
  <div ref="chartRef" class="chart-container"></div>
</template>

<script setup>
import { ref, onMounted, onUnmounted, watch } from 'vue';
import * as echarts from 'echarts/core';
import { HeatmapChart } from 'echarts/charts';
import { 
  TitleComponent, 
  TooltipComponent, 
  VisualMapComponent,
  GridComponent
} from 'echarts/components';
import { CanvasRenderer } from 'echarts/renderers';

// 注册必要的组件
echarts.use([
  TitleComponent, 
  TooltipComponent, 
  VisualMapComponent,
  GridComponent,
  HeatmapChart,
  CanvasRenderer
]);

const props = defineProps({
  title: {
    type: String,
    default: '热力图'
  },
  data: {
    type: Array,
    required: true
  },
  mapWidth: {
    type: Number,
    default: 200
  },
  mapHeight: {
    type: Number,
    default: 100
  },
  maxValue: {
    type: Number,
    default: 100
  },
  colors: {
    type: Array,
    default: () => ['#313695', '#4575b4', '#74add1', '#abd9e9', '#e0f3f8', '#ffffbf', '#fee090', '#fdae61', '#f46d43', '#d73027', '#a50026']
  },
  bgImage: {
    type: String,
    default: ''
  },
  radius: {
    type: Number,
    default: 20
  }
});

const chartRef = ref(null);
let chartInstance = null;

// 初始化图表
const initChart = () => {
  if (!chartRef.value) return;
  
  chartInstance = echarts.init(chartRef.value);
  updateChart();
  
  // 监听窗口变化，调整图表大小
  window.addEventListener('resize', handleResize);
};

// 更新图表数据和配置
const updateChart = () => {
  if (!chartInstance) return;
  
  const option = {
    title: {
      text: props.title,
      textStyle: {
        color: '#fff',
        fontSize: 14
      },
      left: 'center'
    },
    tooltip: {
      position: 'top',
      formatter: (params) => {
        return `坐标: (${params.value[0]}, ${params.value[1]})<br>热度值: ${params.value[2]}`;
      }
    },
    visualMap: {
      min: 0,
      max: props.maxValue,
      calculable: true,
      orient: 'horizontal',
      left: 'center',
      bottom: '5%',
      textStyle: {
        color: 'rgba(255, 255, 255, 0.7)'
      },
      inRange: {
        color: props.colors
      }
    },
    grid: {
      top: '60',
      left: '5%',
      right: '5%',
      bottom: '15%',
      containLabel: true
    },
    xAxis: {
      type: 'value',
      min: 0,
      max: props.mapWidth,
      position: 'top',
      splitLine: {
        show: false
      },
      axisLine: {
        show: false
      },
      axisTick: {
        show: false
      },
      axisLabel: {
        show: false
      }
    },
    yAxis: {
      type: 'value',
      min: 0,
      max: props.mapHeight,
      splitLine: {
        show: false
      },
      axisLine: {
        show: false
      },
      axisTick: {
        show: false
      },
      axisLabel: {
        show: false
      }
    },
    series: [
      {
        name: '热度',
        type: 'heatmap',
        data: props.data,
        pointSize: props.radius,
        blurSize: props.radius,
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  };

  // 设置背景图
  if (props.bgImage) {
    option.backgroundColor = {
      image: props.bgImage,
      repeat: 'no-repeat',
      x: 'center',
      y: 'center'
    };
  }

  chartInstance.setOption(option);
};

// 监听数据变化
watch(() => props.data, () => {
  updateChart();
}, { deep: true });

// 调整图表大小
const handleResize = () => {
  chartInstance && chartInstance.resize();
};

// 组件挂载时初始化图表
onMounted(() => {
  initChart();
});

// 组件卸载前销毁图表实例
onUnmounted(() => {
  if (chartInstance) {
    chartInstance.dispose();
    chartInstance = null;
  }
  window.removeEventListener('resize', handleResize);
});
</script>

<style scoped>
.chart-container {
  width: 100%;
  height: 100%;
  min-height: 300px;
}
</style> 